package com.itheima.mp.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.query.UserQuery;
import com.itheima.mp.domain.vo.UserVO;
import com.itheima.mp.service.IUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.ListIterator;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    IUserService userService;

    /**
     * 根据id批量查询
     * @param ids
     * @return
     */
    @GetMapping
    public List<UserVO> listByIds(@RequestParam("ids")List<Long>ids){
        List<User> users = userService.listByIds(ids);
        List<UserVO> userVOS = BeanUtil.copyToList(users, UserVO.class);
        return userVOS;
    }

    /**
     * 扣balance
     * @param id
     * @param money
     */
    @GetMapping("/{id}/deduction/{money}")
    public void deductBalance(@PathVariable("id")Long id,@PathVariable("money")Integer money){
        userService.deductBalance(id,money);
    }

    @GetMapping("/{id}")
    public UserVO queryUserAndAddressById(@PathVariable("id")Long id){
        return userService.queryUserAndAddressById(id);
    }

    /**
     * 用户条件分页查询
     */
    @GetMapping("/page")
    public PageDTO<UserVO> userPage(UserQuery userQuery){
        return userService.userPage(userQuery);
    }
}
